﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel.Description;
using System.ServiceModel.Dispatcher;
using IAOI.Platform.WCF.WCF.Fault;
using IAOI.Platform.Common.Configuration;
using IAOI.Platform.Common.Diagnostics;

namespace IAOI.Platform.WCF.WCF.Dispatch
{
    public class ServiceBehavior : IServiceBehavior
    {
        public ServiceDispatch ServiceDispatch
        {
            get;
            set;
        }
        //public bool IsParameterLog
        //{
        //    get;
        //    set;
        //}
        //public bool IsMessageLog
        //{
        //    get;
        //    set;
        //}
        //public bool IsErrorHandler
        //{
        //    get;
        //    set;
        //}
        //public ServiceBehavior()
        //{
        //}

        public void AddBindingParameters(ServiceDescription serviceDescription, System.ServiceModel.ServiceHostBase serviceHostBase, System.Collections.ObjectModel.Collection<ServiceEndpoint> endpoints, System.ServiceModel.Channels.BindingParameterCollection bindingParameters)
        {
            return;

        }

        public void ApplyDispatchBehavior(ServiceDescription serviceDescription, System.ServiceModel.ServiceHostBase serviceHostBase)
        {

            foreach (ChannelDispatcher chDisp in serviceHostBase.ChannelDispatchers)
            {

             //   if (this.IsErrorHandler)
            //        chDisp.ErrorHandlers.Add(new ErrorHandler());

                if (ConfigurationManager.Settings.Trace.Level != TraceLevel.Off ||
                    ConfigurationManager.Settings.MessageLogging.Level != TraceLevel.Off)
                {
                    foreach (EndpointDispatcher epDisp in chDisp.Endpoints)
                    {
                        if (ConfigurationManager.Settings.Trace.Level != TraceLevel.Off)
                            epDisp.DispatchRuntime.MessageInspectors.Add(new MessageInspector());

                        if (ConfigurationManager.Settings.MessageLogging.Level != TraceLevel.Off)
                        {
                            foreach (DispatchOperation op in epDisp.DispatchRuntime.Operations)
                                op.ParameterInspectors.Add(new ParameterInspector());
                        }
                    }
                }
            }
        }

        public void Validate(ServiceDescription serviceDescription, System.ServiceModel.ServiceHostBase serviceHostBase)
        {
            //Console.WriteLine("Validate is called.");
            //foreach (ServiceEndpoint se in serviceDescription.Endpoints)
            //{
            //    // Must not examine any metadata endpoint.
            //    if (se.Contract.Name.Equals("IMetadataExchange")
            //      && se.Contract.Namespace.Equals("http://schemas.microsoft.com/2006/04/mex"))
            //        continue;
            //    foreach (OperationDescription opDesc in se.Contract.Operations)
            //    {
            //        if (opDesc.Faults.Count == 0)
            //            throw new InvalidOperationException(String.Format(
            //              "EnforceGreetingFaultBehavior requires a "
            //              + "FaultContractAttribute(typeof(GreetingFault)) in each operation contract.  "
            //              + "The \"{0}\" operation contains no FaultContractAttribute.",
            //              opDesc.Name)
            //            );
            //        bool gfExists = false;
            //        foreach (FaultDescription fault in opDesc.Faults)
            //        {
            //            if (fault.DetailType.Equals(typeof(GreetingFault)))
            //            {
            //                gfExists = true;
            //                continue;
            //            }
            //        }
            //        if (gfExists == false)
            //        {
            //            throw new InvalidOperationException(
            //    "EnforceGreetingFaultBehavior requires a FaultContractAttribute(typeof(GreetingFault)) in an operation contract."
            //            );
            //        }
            //    }
            //}

        }
    }
}
